package com.jike.noobmoney.mvp.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.InviteListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.InviteListEntity;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Create by xuke on 2018/11/10
 * 邀请好友列表
 */
public class InviteListActivity extends BaseActivity implements IView {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.empty)
    ImageView empty;


    List<InviteListEntity.UserinvitelistBean> mData = new ArrayList<>();

    private InviteListAdapter adapter;
    private UserPresenter userPresenter;
    private String userid;

    @Override
    protected int layoutResID() {
        return R.layout.activity_invite_list;
    }

    @Override
    protected void initData() {
        tvTitle.setText("邀请好友列表");
        userPresenter = new UserPresenter(this);
        //adapter
        adapter = new InviteListAdapter(this);
        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        loadData();

    }

    /**
     * 获取数据
     */
    private void loadData() {
        showProgress();
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        userPresenter.userinvitelist(userid, ConstantValue.RequestKey.invite_list);
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.invite_list.equals(actionType)) {
            List<InviteListEntity.UserinvitelistBean> data1 = (List<InviteListEntity.UserinvitelistBean>) data;
            if (data1 != null && data1.size() > 0) {
                mData.clear();
                mData.addAll(data1);
            }


            if (mData.size() > 0) {
                empty.setVisibility(View.GONE);
            } else {
                empty.setVisibility(View.VISIBLE);
            }


            adapter.refresh(mData);
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
        ToastUtils.showShortToastSafe(msg);
    }
}
